package com.homegoods.designer.ui.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.designer.R;
import com.homegoods.designer.databinding.DesignerActivityEditDesignCase2Binding;
import com.homegoods.designer.event.EditSpaceEvent;
import com.homegoods.designer.item.EditDesignCase2Item;
import com.homegoods.designer.presenter.EditDesignCase2Presenter;
import com.homegoods.designer.ui.fragments.AddSpaceFragment;
import com.homegoods.designer.ui.fragments.StatementDialogFragment;
import com.homegoods.aroutercomment.ARouterPath;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.base.MyBaseFragmentActivity;
import com.kedll.library.crop.TakePhotoHelper;
import com.kedll.library.mvp.BaseView;
import com.kedll.library.util.BindingUtil;
import com.kedll.library.util.CollectionUtils;
import com.kedll.library.util.RxBus;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.RuntimePermissions;

/**
 * @author QiQi
 * @description 编辑作品2
 */
@RuntimePermissions
@Route(path = ARouterPath.PATH_EDIT_CASE_2)
public class EditDesignCase2Activity extends MyBaseFragmentActivity<DesignerActivityEditDesignCase2Binding, EditDesignCase2Presenter> implements BaseView {

    private TakePhotoHelper photoHelper;

    @Override
    public int getLayout() {
        return R.layout.designer_activity_edit_design_case2;
    }

    @SuppressLint("CheckResult")
    @Override
    protected void initEvent() {
        //设置封面
        onClicked(mBinding.ivCover, o -> EditDesignCase2ActivityPermissionsDispatcher.onTakePhotoWithPermissionCheck(this));

        //添加空间
        onClicked(mBinding.llAddSpace, o -> {
            AddSpaceFragment addSpaceFragment = new AddSpaceFragment();
            addSpaceFragment.setOnConfirmClickListener(spaceName -> mPresenter.addSpace(spaceName));
            addSpaceFragment.show(getSupportFragmentManager(), "addSpace");
        });

        photoHelper = new TakePhotoHelper(this);
        photoHelper.setOnTakePhotoCompleteListener(picStr -> BindingUtil.loadImageNoTransform(mBinding.ivCover, CollectionUtils.get(picStr, 0), null, null));

        mPresenter.getAdapter().setOnItemClickListener(((MultiTypeAdapter.OnItemClickListener<EditDesignCase2Item>) (v, iItem) -> ARouter.getInstance()
                .build(ARouterPath.PATH_EDIT_SPACE)
                .withObject("space", iItem.getData())
                .withInt("index", mPresenter.getAdapter().findPos(iItem))
                .navigation()));

        RxBus.getDefault().toFlowable(EditSpaceEvent.class)
                .compose(bindToLifecycle())
                .subscribe(editSpaceEvent -> mPresenter.updateSpace(editSpaceEvent));
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EditDesignCase2ActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        photoHelper.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mBinding.spaceList.setAdapter(mPresenter.getAdapter());
        mBinding.spaceList.setNestedScrollingEnabled(false);
        mPresenter.initData();
    }

    @Override
    protected void initToolBar(Bundle savedInstanceState) {
        super.initToolBar(savedInstanceState);
        setTitle("编辑作品");
    }

    @Override
    protected EditDesignCase2Presenter getPresenter() {
        return new EditDesignCase2Presenter(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.designer_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.preview) {
            Toast.makeText(this, "预览", Toast.LENGTH_SHORT).show();
            return true;
        } else if (item.getItemId() == R.id.submit) {
            new StatementDialogFragment().showDialog(getSupportFragmentManager(), "statement");
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @NeedsPermission({Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void onTakePhoto() {
        photoHelper.showPopup();
    }
}
